using QFramework;
using UnityEngine;

namespace QFramework.Monk
{
    public abstract class PowerUp : GameEntityObj,IController
    {
        protected ConfigModel mConfigModel;
        protected virtual void Awake()
        {
            mConfigModel = this.GetModel<ConfigModel>();
        }
        public bool FlyingToPlayer { get; set; }

        private int FlyingToPlayerFrameCount = 0;

        protected abstract void Execute();

        protected virtual void OnTriggerEnter2D(Collider2D other)
        {
            if (other.gameObject.layer== LayerMask.NameToLayer("Player"))
            {
                FlyingToPlayer = CanTriggerFly();
            }
        }
        
        private void Update()
        {
            if (FlyingToPlayer)
            {
                if (FlyingToPlayerFrameCount == 0)
                {
                    GetComponent<SpriteRenderer>().sortingOrder = 5;
                }

                FlyingToPlayerFrameCount++;

                if (Player.Default)
                {
                    var direction = Player.Default.DirectionFrom(this);
                    var distance = direction.magnitude;

                    if (FlyingToPlayerFrameCount <= 15)
                    {
                        transform.Translate(direction.normalized * -2 * Time.deltaTime);
                    }
                    else
                    {
                        transform.Translate(direction.normalized * 7.5f * Time.deltaTime);
                    }

                    if (distance < 0.1f)
                    {
                        Execute();
                    }
                }
                
            }
            
        }
        
        public IArchitecture GetArchitecture()
        {
            return  AppStart.Interface;
        }


        protected virtual bool CanTriggerFly()
        {
            return true;
        }
    }
}